package service;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

import dao.DaoImp_Rent;
import dao.IDao_Rent;
import model.CommonList;
import model.RentCost;
import model.RentPrice;
import model.RentReminder;

public class ServiceImp_Rent implements IService_Rent
{
	private IDao_Rent dao_Rent;

	public ServiceImp_Rent(IDao_Rent dao_Rent)
	{
		this.dao_Rent=dao_Rent;
	}

	// private CommonList common;

	// public ServiceImp_Rent()
	// {
	// daoRent = new DaoImp_Rent();
	// // common = CommonList.getInstance();
	// }

	public int setRent(RentPrice rentPrice)
	{
		return dao_Rent.setRent(rentPrice);
	}

	public List<RentCost> queryByName(String name)
	{
		List<RentCost> list = dao_Rent.queryByName(name);
		if (list.isEmpty())
		{
			list.add(getInitRentCost(name));
		}
		return list;
	}

	private RentCost getInitRentCost(String name)
	{

		return dao_Rent.getInitRentCost(name);
	}

	public List<RentCost> queryByTeacherName(String teacherName)
	{
		return dao_Rent.queryByTeacherName(teacherName);
	}

	public List<RentCost> queryAll()
	{
		List<RentCost> list = dao_Rent.queryAll();
		Map<String, Integer> map = CommonList.getInstance().getStudentName();
		if (list.size() >= map.size())
		{
			return list;
		}
		else
		{
			List<RentCost> newlist = new ArrayList<RentCost>();
			Vector<String> vector = new Vector<String>(map.keySet());
			for (int i = 0; i < vector.size(); i++)
			{
				// List<RentCost> single =
				newlist.add(queryByName(vector.get(i)).get(0));
			}
			return newlist;
		}
	}

	public Properties getProperties()
	{
		return dao_Rent.getProperties();
	}

	public List<RentReminder> getReminderList(int days)
	{
		List<RentCost> list = queryAll();
		List<RentReminder> result = new ArrayList<RentReminder>();
		int rent = CommonList.getInstance().getRentPerMonth();
		long date = System.currentTimeMillis() + 7 * dayMilliSeconds;
		for (int i = 0; i < list.size(); i++)
		{
			RentCost rentCost = list.get(i);
			RentReminder reminder = new RentReminder(rentCost);
			reminder.setRentPerMonth(rent);
			if (reminder.isQualified(date))
			{
				result.add(reminder);
			}
		}
		return result;
	}

	public Vector<Vector<Object>> cut(Vector<Vector<Object>> data,
			boolean isCutSingle, boolean isNameLess, int rowIndex,
			Clipboard clipboard)
	{
		String s = "";
		if (isCutSingle)
		{
			if (isNameLess)
			{
				s = data.get(rowIndex).get(1).toString() + "\t"
						+ data.get(rowIndex).get(2).toString();
			}
			else
			{
				s = data.get(rowIndex).get(0).toString() + "\t"
						+ data.get(rowIndex).get(1).toString() + "\t"
						+ data.get(rowIndex).get(2).toString();
			}
			data.remove(rowIndex);
		}
		else
		{
			String teacher = data.get(rowIndex).get(0).toString();
			int size = data.size();
			if (isNameLess)
			{
				for (int i = 0; i < size; i++)
				{
					if (data.get(size - i - 1).get(0).toString()
							.equals(teacher))
					{
						s += data.get(size - i - 1).get(1).toString() + "\t"
								+ data.get(size - i - 1).get(2).toString()
								+ "\n";
						data.remove(size - i - 1);
					}
				}
			}
			else
			{
				for (int i = 0; i < size; i++)
				{
					if (data.get(size - i - 1).get(0).toString()
							.equals(teacher))
					{
						s += data.get(size - i - 1).get(0).toString() + "\t"
								+ data.get(size - i - 1).get(1).toString()
								+ "\t"
								+ data.get(size - i - 1).get(2).toString()
								+ "\n";
						data.remove(size - i - 1);
					}
				}
			}
		}
		StringSelection selection = new StringSelection(s);
		clipboard.setContents(selection, null);
//		button_cut.setEnabled(false);
//		table.updateUI();
		return data;
	}

	private final static long dayMilliSeconds = 24 * 60 * 60 * 1000;

}
